package udes.assassin;

import java.util.List;

import udes.assassin.structure.Player;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class PlayerListAdapter extends ArrayAdapter<Player>{
	
	private final List<Player> list;
	private final Activity context;
	
	public PlayerListAdapter(Activity context, List<Player> list) {
	    super(context, R.layout.player_description, list);
	    this.context = context;
	    this.list = list;
	  }

	  static class ViewHolder {
	    protected TextView playerName;
	    protected CheckBox isReady;
	  }
	  
	  @Override
	  public View getView(int position, View convertView, ViewGroup parent) {
		  
	    View view = null;
	    
	    if (convertView == null) {
	      LayoutInflater inflator = context.getLayoutInflater();
	      view = inflator.inflate(R.layout.player_description, null);
	      final ViewHolder viewHolder = new ViewHolder();
	      viewHolder.playerName = (TextView) view.findViewById(R.id.player_name);
	      viewHolder.isReady = (CheckBox) view.findViewById(R.id.is_ready);
	      view.setTag(viewHolder);
	    } 
	    else {
	      view = convertView;
	    }
	    ViewHolder holder = (ViewHolder) view.getTag();
	    holder.playerName.setText(list.get(position).getName());
	    holder.isReady.setChecked(list.get(position).isReady());
	    return view;
	  }
}
